package fetcher

import (
	"bytes"
	"crypto/tls"
	"fmt"
	"io/ioutil"
	"net/http"
	"spider/common"
)

//根据url,头信息 获取结果,不做解析
func FetchGet(url string,header map[string] string,s string)(res []byte,err error)  {
	var (
		req *http.Request
	)
	header=common.MergreHeader(header,common.CommonHeader)

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		//Proxy:http.ProxyURL(common.G_abuyun.GetProxy()),
	}
	client := http.Client{Transport:tr}

	if req, err = http.NewRequest(http.MethodGet, url, nil); err != nil {
		panic(err)
	}
	for k,v:=range header{
		req.Header.Add(k, v)
	}
	resp, err := client.Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode!=http.StatusOK{
		err=fmt.Errorf("    wrong status code %d",resp.StatusCode)
		return
	}
	res,err=ioutil.ReadAll(resp.Body)
	return
}


//美团资质post请求
func MeiTuanPost(url string,header map[string]string,postStr string)(res []byte, err error)  {

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		//Proxy:http.ProxyURL(common.G_abuyun.GetProxy()),
	}
	client := http.Client{Transport:tr}

	req,err:=http.NewRequest(http.MethodPost,url,bytes.NewBufferString(postStr))
	if err!=nil{
		return
	}

	for k,v:=range header{
		req.Header.Add(k,v)
	}
	resp, err := client.Do(req)
	if err != nil {
		return
	}

	defer resp.Body.Close()

	if resp.StatusCode!=http.StatusOK{
		err=fmt.Errorf("    wrong status code %d",resp.StatusCode)
		return
	}

	res,err=ioutil.ReadAll(resp.Body)
	return
}

